package Control;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

import montecarlo.MultiThreadMontCarlo;
import montecarlo.SingleThreadMontCarlo;


//temporary controlleur use to test montecarlo algorithm
//example of using pool

public class Controlleur implements Runnable{
	
	//variable
	final private int nSimulation;
	final boolean tmp;
	
	//Main
	public static void main(String[] args) 
			throws Throwable {
		// TODO Auto-generated method stub
		Thread th1=new Thread(new Controlleur(true));
		Thread th2=new Thread(new Controlleur(false));
		th1.start();
		th2.start();

	}
	
	//constructor
	Controlleur(boolean t){
		//determine final variable
		   nSimulation=20;
		   tmp=t;
	}
	
	public void run(){
		try {
			timeAndComputeValue();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//function of test which return a informations
	  public void timeAndComputeValue() 
			    throws ExecutionException, IOException, InterruptedException {
		  double result=0.0;
			    final long start = System.nanoTime();
			    //double result = PoolThreading();    
			    if(tmp)result =Calculation();
			    else{
			    	result=CalculationMultithread();
			    }
			    		
			    final long end = System.nanoTime();
			    System.out.println("FinalResult= " + result);
			   	System.out.println("Time (s) : " + (end - start)/1.0e9);
			    
		  		}

	  
	  
	  private double Calculation() throws ExecutionException, InterruptedException{
		  
		  double resultFinal = 0.0; 
		    SingleThreadMontCarlo tmp=new SingleThreadMontCarlo();
		    
		  for(int i=0;i<nSimulation;i++) {
			  resultFinal+=tmp.call();
		  }
		  
		  
		  return resultFinal;
	  }
	  private double CalculationMultithread() throws ExecutionException, InterruptedException{
		  
		  double resultFinal = 0.0; 
		  MultiThreadMontCarlo tmp= new MultiThreadMontCarlo();
		    
		  for(int i=0;i<nSimulation;i++) {
			  resultFinal+=tmp.call();
		  }
		  
		  
		  return resultFinal;
	  }
	
}
